package com.ihrm.common.handler;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author chahua
 * @date 2022/3/26 17:34
 **/
@ControllerAdvice
public class BaseExceptionHandler{

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e){
        if(e.getClass() == CommonException.class){
            CommonException commonException = (CommonException) e;
            return new Result(commonException.getResultCode());
        }
        return new Result(ResultCode.SERVER_ERROR);
    }
}
